package cn.com.surker.sale.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.sale.entity.SaleContractTicket;
import cn.com.surker.sale.dao.SaleContractTicketDao;
import cn.com.surker.sale.service.SaleContractTicketBaseService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* <p>
    * 销售合同开票信息表 服务实现类
    * </p>
*
* @author admin
* @since 2024-08-03
*/
public abstract class SaleContractTicketBaseServiceImpl extends ServiceImpl<SaleContractTicketDao, SaleContractTicket> implements SaleContractTicketBaseService {

    @Resource
    protected SaleContractTicketDao contractTicketDao;

    /**
     * 分页查询
     * @param ticket
     * @return
     */
    public IPage findPage(SaleContractTicket ticket) {
        PageHelper.offsetPage(ticket.getOffset(), ticket.getLimit());
        QueryWrapper<SaleContractTicket> query = new QueryWrapper<>();
        if (StringUtils.isNull(ticket.getStatus()))
            ticket.setStatus(0);
        if (StringUtils.isNull(ticket.getOrgId()))
            query.eq("org_id", ticket.getOrgId());
        if (!StringUtils.isNull(ticket.getTicketPerson()))
            query.inSql("ticket_person", "select id from sale_contract where sales ='"
                    + ticket.getTicketPerson() + "'");
        query.eq("status", ticket.getStatus());
        if (!StringUtils.isNull(ticket.getStartTime()))
            query.ge("ticket_time", ticket.getStartTime());
        if (!StringUtils.isNull(ticket.getEndTime()))
            query.lt("ticket_time", ticket.getEndTime());
        if (!StringUtils.isNull(ticket.getSearchVal()))
            query.and(orderWrapper -> orderWrapper.like("name", ticket.getSearchVal()).or()
                    .like("process", ticket.getSearchVal()).or().like("contract_name", ticket.getSearchVal())
                    .or().like("tax_code", ticket.getSearchVal()).or().like("ticket_person", ticket.getSearchVal())
                    .or().like("creator", ticket.getSearchVal()).or().like("remark", ticket.getSearchVal()));
        query.orderByDesc("sign_date");

        IPage<SaleContractTicket> iPage = contractTicketDao.selectPage(new Page<>((ticket.getOffset() + ticket.getLimit()) / ticket.getLimit(), ticket.getLimit()), query);
        /**分页插件**/
        if (!StringUtils.isNull(iPage.getRecords())) {
            PageInfo pageInfo = new PageInfo(iPage.getRecords());
            if (!StringUtils.isNull(pageInfo.getTotal()))
                iPage.setTotal(pageInfo.getTotal());
        }
        return iPage;
    }

    /**
     * 新增开票信息
     * @param ticket
     * @return
     */
    public Integer saveTicket(SaleContractTicket ticket) {
        if (ticket == null)
            return Code.PARAM_EMPTY.getCode();
        Integer code = contractTicketDao.insert(ticket);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 更新开票数据
     * @param ticket
     * @return
     */
    public Integer updateTicket(SaleContractTicket ticket) {
        if (ticket == null)
            return Code.PARAM_EMPTY.getCode();
        Integer code = contractTicketDao.updateById(ticket);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 更新开票状态
     * @param id
     * @param status
     * @param creator
     * @return
     */
    public Integer updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            return Code.PARAM_EMPTY.getCode();
        SaleContractTicket ticket = contractTicketDao.selectById(id);
        if (ticket == null)
            return Code.ERROR_DATA.getCode();
        ticket.setUpdateTime(new Date());
        ticket.setUpdator(creator);
        ticket.setStatus(status);
        Integer code = contractTicketDao.updateById(ticket);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 查询合同时间段产值
     * @param searchVal
     * @param orgId
     * @param startTime 开始时间 yyyy-mm-dd
     * @param endTime 结束时间 yyyy-mm-dd
     * @return
     */
    public List<Map<String, Object>> findContractYield(String searchVal, Long orgId, String startTime, String endTime) {
        if (StringUtils.isNull(searchVal))
            searchVal = "";
        if (StringUtils.isNull(orgId))
            return null;
        List<Map<String, Object>> list = contractTicketDao.findContractYield(searchVal, orgId, startTime, endTime);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

    /**
     * 新增工地
     * @param searchVal
     * @param orgId
     * @param startTime 开始时间 yyyy-mm-dd
     * @param endTime 结束时间 yyyy-mm-dd
     * @return
     */
    public List<Map<String, Object>> findNewContract(String searchVal, Long orgId, String startTime, String endTime, Integer type) {
        if (StringUtils.isNull(searchVal))
            searchVal = "";
        if (StringUtils.isNull(orgId))
            return null;
        String sort = "c.name,a.business_quality,a.create_time desc";
        if (!StringUtils.isNull(type) && type == 0)
            sort = "a.create_time desc";
        List<Map<String, Object>> list = contractTicketDao.findNewContract(searchVal, orgId, startTime, endTime, sort);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }

}